package com.bw.mvp.base;

import java.lang.ref.WeakReference;

/**
 * 业务层：内存泄露问题
 */
public class BasePresenter<V> {
    WeakReference<V> mWeakReference; //弱引用

    /**
     * 绑定view
     * @param view
     */
    public void attachView(V view)
    {
        mWeakReference = new WeakReference<>(view);
    }

    /**
     * 解绑view
     */
    public void detachView()
    {
        //当页面销毁的时候，将弱引用置空，GC将view回收
        if (mWeakReference != null)
        {
            mWeakReference.clear();
            mWeakReference = null;
        }
    }

    /**
     * 获得绑定的view
     * @return
     */
    public V getView()
    {
        return mWeakReference.get();
    }

    /**
     * 判断是否绑定view
     * @return
     */
    public boolean isViewAttached()
    {
        return mWeakReference != null && mWeakReference.get() != null;
    }

}
